/*
 * Copyright (c) 2014 pkzd/1.0, 颜文泽(robin)
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     1 Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     2 Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include	<asm.h>
#include	<term.h>
#include	<param.h>
#include	<string.h>

static void	mc_putint(int x, int b);
static void	mc_putuint(uint x, int b);

static ushort	*term = (ushort *)(VGA_ADDR);

static void
mc_putint(int x, int b)
{
	int a;
	
	if(x < 0){
		mc_putc('-');
		x = -x;			
	}	
	if(a = x / b)
		mc_putint(a, b);
	mc_putc("0123456789abcdef"[x % b]);
}

static void
mc_putuint(uint x, int b)
{
	uint a;

	if(a = x / b)
		mc_putuint(a, b);
	mc_putc("0123456789abcdef"[x % b]);
}

void
mc_terminit(void)
{
	outb(VGA_MEM_H, VGA_CMD);
	outb(0x00, VGA_DAT);
	outb(VGA_MEM_L, VGA_CMD);
	outb(0x00, VGA_DAT);
	memset((uchar *)term, 0, VGA_PER_SIZ);	
	outb(VGA_MEM_H, VGA_CMD);
	outb(0x00, VGA_DAT);
	outb(VGA_MEM_L, VGA_CMD);
	outb(0x00, VGA_DAT);
	term[0] = ' ' | VGA_BLACK_WHITE;
}

void
mc_putc(char c)
{
	uint pos;

	outb(VGA_CUR_H, VGA_CMD);
	pos = inb(VGA_DAT);
	outb(VGA_CUR_L, VGA_CMD);
	pos = (pos << 8) | inb(VGA_DAT);
	switch(c){
	case '\n':
		pos += VGA_COL_SIZ - pos % VGA_COL_SIZ;
		break;
	case '\t':
		pos += VGA_TAB_SIZ - pos % VGA_TAB_SIZ;
		break;
	default:	
		term[pos++] = c | VGA_BLACK_WHITE;
		break;
	}
	if(pos == VGA_PER_CNT){
		memmove((uchar *)term, (uchar *)(term + VGA_COL_SIZ), (VGA_PER_CNT - VGA_COL_SIZ) * 2);
		pos -= VGA_COL_SIZ;
		memset((uchar*)(term + pos), 0, VGA_COL_SIZ * 2);
	}
	outb(VGA_CUR_H, VGA_CMD);
	outb((uchar)(pos >> 8), VGA_DAT);
	outb(VGA_CUR_L, VGA_CMD);
	outb((uchar)(pos), VGA_DAT);
	term[pos] = ' ' | VGA_BLACK_WHITE;
}

void
mc_puts(const char *s)
{
	const char *p;

	for(p = s; *p; p++)
		mc_putc(*p);
}

void
mc_printf(const char *fmt, ...)
{
	uint *addr;
	const char *p;

	addr = (uint *)(&fmt) + 1;
	for(p = fmt; *p; p++){
		switch(*p){
		case '%':
			switch(*++p){
			case 'd':
			case 'i':
				mc_putint(*addr++, 10);	
				break;
			case 'x':
			case 'u':
			case 'o':
				mc_putuint(*addr++, *p == 'u' ? 10 : ((*p == 'x' || *p == 'X') ? 16 : 8));
				break;
			case 's':
				mc_puts((uchar *)(*addr++));
				break;
			case 'c':
				mc_putc(*addr++);
				break;
			case '%':
				mc_putc(*p);
				break;
			default:
				mc_putc(*--p);
				break;
			}
			break;
		default:
			mc_putc(*p);
			break;
		}
	}
}

void
mc_spin(const char *s)
{
	if(s)
		mc_puts(s);
	for( ; ; )
		;
}
